<?php
/**
 * @copyright Copyright (c) 2021 深圳市酷瓜软件有限公司
 * @license https://opensource.org/licenses/GPL-2.0
 * @link https://www.koogua.com
 */

namespace App\Http\Admin\Controllers;

use App\Http\Admin\Services\Agency;
use App\Http\Admin\Services\Article as ArticleService;

/**
 * @RoutePrefix("/admin/agency")
 */
class AgencyController extends Controller
{
    /**
     * @Get("/index", name="admin.agency.index")
     */
    public function indexAction()
    {
        $service = new Agency();
        $pager = $service->getAgencies();
        return $this->view->setVar('pager', $pager);
    }

    /**
     * @Get("/add", name="admin.agency.add")
     */
    public function addAction()
    {
    }

    /**
     * @Get("/{id:[0-9]+}/edit", name="admin.agency.edit")
     */
    public function editAction($id)
    {
        $service = new Agency();
        $agency = $service->getAgency($id);
        $this->view->setVar('agency', $agency);
    }

    /**
     * @Get("/{id:[0-9]+}/show", name="admin.agency.show")
     */
    public function showAction($id)
    {
    }

    /**
     * @Post("/create", name="admin.agency.create")
     */
    public function createAction()
    {
        $service = new Agency();
        $service->createAgency();
        $content = [
            'location' => $this->url->get(['for' => 'admin.agency.index']),
            'msg'      => '添加机构成功',
        ];
        return $this->jsonSuccess($content);
    }

    /**
     * @Post("/{id:[0-9]+}/update", name="admin.agency.update")
     */
    public function updateAction($id)
    {
        $service = new Agency();
        $service->updateAgency($id);
        $content = [
            'location' => $this->url->get(['for' => 'admin.agency.index']),
            'msg'      => '修改机构成功',
        ];
        return $this->jsonSuccess($content);
    }

    /**
     * @Post("/{id:[0-9]+}/delete", name="admin.agency.delete")
     */
    public function deleteAction($id)
    {
        $articleService = new Agency();
        $articleService->deleteAgency($id);
        $content = [
            'location' => $this->url->get(['for' => 'admin.agency.index']),
            'msg'      => '删除机构成功',
        ];
        return $this->jsonSuccess($content);
    }

}